<?php
require_once 'phemto/phemto.php';
require_once 'Bucket/bucket.inc.php';

class ss_factory{
	protected $settings;
	protected $singeltons=array();
	
	public function __construct($settings){
		$this->settings=$settings;
	}
	
	
	public function new_site_templatefactory(){
		return new site_templatefactory($template_dir=realpath(SS_APP_PATH . '/templates'),$template_dir . '_c');
	}
	
	public function new_ss_dao_trans(){
		return new ss_dao_trans(
			$this->settings->dao->default->type.':host='.$this->settings->dao->default->host.';dbname='.$this->settings->dao->default->database,
			$this->settings->dao->default->user,
			$this->settings->dao->default->pass,
			$this->settings->dao->default->conParam
			);
	}
	
	protected function getSingelton($class){
		if(array_key_exists($class,$this->singeltons)) return $this->singeltons[$class];
		return false;
	}
	/*
	public function new_ss_dao_structure(){
		return new ss_dao_structure('')
	}
	*/
}

function create_bucket_container($ss) {
	ss_error::log('Building injector');
	$bucket=new ss_core_bucketInjector(new ss_factory($ss));
	$bucket->set($ss,'ss_core_settings');
	$bucket->registerImplementation('ss_dao_da_interface','ss_dao_trans');
	$bucket->registerImplementation('ss_core_injectorInterface','ss_core_bucketInjector');
	$bucket->set($bucket,'ss_core_injectorInterface');
	return $bucket;
}

function create_phemto_container($ss) {
  $GLOBALS['injector']=$injector = new Phemto();
  // put application wiring here
  $template_dir = realpath(SS_APP_PATH . '/templates');
  //ss_error::log($template_dir);
  $compile_dir = $template_dir . '_c';
  
  
	$injector->whenCreating('site_templatefactory')->forVariable('template_dir')->willUse(new Value($template_dir));
	$injector->whenCreating('site_templatefactory')->forVariable('compile_dir')->willUse(new Value($compile_dir));
	$injector->whenCreating('ss_dao_trans')->forVariable('connection')->willUse(new Value($ss->dao->default->type.':host='.$ss->dao->default->host.';dbname='.$ss->dao->default->database));
	$injector->whenCreating('ss_dao_trans')->forVariable('user')->willUse(new Value($ss->dao->default->user));
	$injector->whenCreating('ss_dao_trans')->forVariable('pass')->willUse(new Value($ss->dao->default->pass));
	$injector->whenCreating('ss_dao_trans')->forVariable('conParam')->willUse(new Value($ss->dao->default->conParam));
	$injector->forVariable('ss_settings')->willUse($ss);
	$injector->forType('site_dao_structure_mapper')->call('setInjector');
	$injector->forType('site_dao_entry_mapper')->call('setInjector');
	$injector->forType('site_dao_category_mapper')->call('setInjector');
	$injector->forType('site_dao_office_mapper')->call('setInjector');
	$injector->forType('site_dao_staff_mapper')->call('setInjector');
	//$injector->forType('ss_konstrukt_component')->call('settings');
	$injector->willUse($injector);
	$injector->willUse(new Reused('ss_dao_trans'));
	$injector->willUse(new Reused('site_dao_entry_dao'));
	$injector->willUse(new Reused('site_dao_entry_model'));
	$injector->willUse(new Reused('site_dao_structure_mapper'));
	$injector->willUse(new Reused('site_dao_category_mapper'));
	$injector->willUse(new Reused('site_dao_office_mapper'));
	$injector->willUse(new Reused('site_dao_staff_mapper'));
	$injector->willUse(new Reused('site_dao_structure'));
	$injector->willUse(new Reused('site_dao_structuretype'));
	$injector->willUse(new Reused('site_dao_businessarea'));
	
	
	$injector->forType('ss_dao_category_mapper')->call('setInjector');
	$injector->willUse(new Reused('ss_dao_category_mapper'));
	$injector->forType('ss_dao_structure_mapper')->call('setInjector');
	$injector->willUse(new Reused('ss_dao_structure_mapper'));
	$injector->forType('ss_dao_staff_mapper')->call('setInjector');
	$injector->willUse(new Reused('ss_dao_staff_mapper'));
	$injector->forType('ss_dao_office_mapper')->call('setInjector');
	$injector->willUse(new Reused('ss_dao_office_mapper'));
	$injector->forType('ss_dao_entry_mapper')->call('setInjector');
	$injector->willUse(new Reused('ss_dao_entry_mapper'));
	$injector->forType('ss_dao_businessarea_mapper')->call('setInjector');
	$injector->willUse(new Reused('ss_dao_businessarea_mapper'));
	$injector->willUse(new Reused('ss_dao_structure'));
	$injector->forType('ss_dao_structuretype_mapper')->call('setInjector');
	$injector->willUse(new Reused('ss_dao_structuretype_mapper'));
	
	
	$injector->create('ss_dao_trans');//->beginTransaction();
	$mapper=$injector->create('site_dao_structure_mapper');
	$injector->willUse($mapper->api_master());
  return $GLOBALS['injector'];
}
